package com.luoke.dao.impl;


import com.luoke.dao.SkillDao;
import com.luoke.pojo.entity.Skill;
import com.luoke.util.DatabaseUtil;

import java.sql.*;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * @author : 叶非凡
 * @date : 2024/11/14 17:05
 */
public class SkillDaoImpl implements SkillDao {
    @Override
    public List<Skill> getSkillByPetNameAndUsername(String userName, String userPetName) {
        String sql = "SELECT " +
                "    s.id, " +
                "    s.name, " +
                "    s.need_mp, " +
                "    s.value " +
                "FROM " +
                "    user u " +
                "INNER JOIN " +
                "    user_pet up ON u.id = up.user_id " +
                "INNER JOIN " +
                "    pet p ON up.pet_id = p.id " +
                "INNER JOIN " +
                "    pet_skill ps ON p.id = ps.pet_id " +
                "INNER JOIN " +
                "    skill s ON ps.skill_id = s.id " +
                "WHERE " +
                "    u.name = ? AND p.name = ?;";

        List<Skill> skills = new ArrayList<>();
        try (Connection conn = DatabaseUtil.getConnection();
             PreparedStatement stmt = conn.prepareStatement(sql)) {

            stmt.setString(1, userName);
            stmt.setString(2, userPetName);


            try (ResultSet rs = stmt.executeQuery()) {
                while (rs.next()) {
                    Skill skill = new Skill();
                    skill.setId(rs.getLong("id"));
                    skill.setName(rs.getString("name"));
                    skill.setNeedMp(rs.getInt("need_mp"));
                    skill.setValue(rs.getInt("value"));
                    skills.add(skill);
                }
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }

        return skills;




    }
}
